package com.useeinfo.oa.modules.maintaining.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Resource;
import com.useeinfo.oa.modules.maintaining.enums.InputTypeEnum;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 维保内容表
 */
@Entity
@Table(name = "maintain_data")
public class MaintainData extends SuperClassEntity<MaintainData> {
    /**
     * 维保项目
     */
    private String name;
    /**
     * 维保类别（轻度保养 中度保养 深度保养）
     */
    private String category;
    /**
     * 应用场景（禁用）
     */
    @ManyToOne
    @JoinColumn(name = "application_scenarios_id")
    private ApplicationScenarios applicationScenarios;
    /**
     * 应用场景(数据字典表)
     */
    @ManyToOne
    @JoinColumn(name = "dict_info_id")
    private DictInfo dictInfo;
    /**
     * 输入类型
     */
    @Enumerated(value = EnumType.STRING)
    private InputTypeEnum inputTypeEnum;
    /**
     * 最大值
     */
    private Double maxNumber;
    /**
     * 最小值
     */
    private Double minNumber;
    /**
     * 数值单位
     */
    private String unit;
    /**
     * 是否需要拍照
     */
    @Column(columnDefinition = "bit default 0")
    private boolean photo;
    /**
     * 维保内容
     * 格式:[{"name":"维保内容","text":"11"},{"name":"维保内容","text":"22"},{"name":"维保内容","text":"33"},{"name":"维保内容","text":"44"},{"name":"维保内容","text":"55"},{"name":"维保内容","text":"66"}]
     */
    @Column(columnDefinition="text")
    private String content;
    /**
     * 附件
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "maintain_data_2_resource", joinColumns = {
            @JoinColumn(name = "maintainData_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> enclosure = new HashSet<Resource>();

    /**
     * 所属工艺
     */
    @ManyToOne
    @JoinColumn(name = "maintain_technology_id")
    private MaintainTechnology maintainTechnology;

    /**
     * 所属企业
     */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    /**
     * 时间戳
     */
    private Long timeStamp;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public ApplicationScenarios getApplicationScenarios() {
        return applicationScenarios;
    }

    public void setApplicationScenarios(ApplicationScenarios applicationScenarios) {
        this.applicationScenarios = applicationScenarios;
    }

    public InputTypeEnum getInputTypeEnum() {
        return inputTypeEnum;
    }

    public void setInputTypeEnum(InputTypeEnum inputTypeEnum) {
        this.inputTypeEnum = inputTypeEnum;
    }

    public Double getMaxNumber() {
        return maxNumber;
    }

    public void setMaxNumber(Double maxNumber) {
        this.maxNumber = maxNumber;
    }

    public Double getMinNumber() {
        return minNumber;
    }

    public void setMinNumber(Double minNumber) {
        this.minNumber = minNumber;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public Boolean getPhoto() {
        return photo;
    }

    public void setPhoto(Boolean photo) {
        this.photo = photo;
    }

    public Set<Resource> getEnclosure() {
        return enclosure;
    }

    public void setEnclosure(Set<Resource> enclosure) {
        this.enclosure = enclosure;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public MaintainTechnology getMaintainTechnology() {
        return maintainTechnology;
    }

    public void setMaintainTechnology(MaintainTechnology maintainTechnology) {
        this.maintainTechnology = maintainTechnology;
    }

    public Long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Long timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public DictInfo getDictInfo() {
        return dictInfo;
    }

    public void setDictInfo(DictInfo dictInfo) {
        this.dictInfo = dictInfo;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("name", StringConverters.null2String(getName()));
        jsonObject.put("category", StringConverters.null2String(getCategory()));
        if(getDictInfo()!=null){
            jsonObject.put("applicationScenarios", StringConverters.null2String(getDictInfo().getPropertyName()));
        }else{
            jsonObject.put("applicationScenarios", "");
        }
        if(getInputTypeEnum()!=null){
            jsonObject.put("inputTypeEnum", StringConverters.null2String(getInputTypeEnum().getName()));
        }else{
            jsonObject.put("inputTypeEnum", "");
        }

        if(getEnterprise()!=null){
            jsonObject.put("enterpriseName", StringConverters.null2String(getEnterprise().getEnterpriseName()));
        }else{
            jsonObject.put("enterpriseName", "");
        }
        if(getMaintainTechnology()!=null){
            jsonObject.put("isTemp", "否");
        }else{
            //临时数据
            jsonObject.put("isTemp", "是");
        }
        return jsonObject;
    }

    //快照专用
    public JSONObject toJSONObject2() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("name", StringConverters.null2String(getName()));
        jsonObject.put("category", StringConverters.null2String(getCategory()));
        jsonObject.put("photo", getPhoto());
        jsonObject.put("maxNumber", getMaxNumber());
        jsonObject.put("minNumber", getMinNumber());
        jsonObject.put("unit", getUnit());
        jsonObject.put("content", getContent());
        if(getDictInfo()!=null){
            jsonObject.put("applicationScenariosName", StringConverters.null2String(getDictInfo().getPropertyName()));
            jsonObject.put("applicationScenariosId", getDictInfo().getId());
        }else{
            jsonObject.put("applicationScenariosName", "");
            jsonObject.put("applicationScenariosId",null);
        }
        if(getInputTypeEnum()!=null){
            jsonObject.put("inputTypeEnumCode", StringConverters.null2String(getInputTypeEnum().getCode()));
            jsonObject.put("inputTypeEnumText", StringConverters.null2String(getInputTypeEnum().getName()));
        }else{
            jsonObject.put("inputTypeEnumCode", "");
            jsonObject.put("inputTypeEnumText", "");
        }

        String resourceIds = "";
        for(Resource resource:enclosure){
            if(resourceIds.equals("")){
                resourceIds=resource.getId().toString();
            }else {
                resourceIds+=","+resource.getId();
            }
        }
        jsonObject.put("resourceIds",resourceIds);
        return jsonObject;
    }

    //列表等简单展示用
    public JSONObject toJSONObject3() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("name", StringConverters.null2String(getName()));
        if(getDictInfo()!=null){
            jsonObject.put("applicationScenariosName", StringConverters.null2String(getDictInfo().getPropertyName()));
            jsonObject.put("applicationScenariosId", getDictInfo().getId());
        }else{
            jsonObject.put("applicationScenariosName", "");
            jsonObject.put("applicationScenariosId",null);
        }
        return jsonObject;
    }
}
